<template>
    <section style="padding-top: 20px;">
        <el-form ref="form" :rules="rules" :model="form" label-width="120px" label-position="right" size="medium">
            <el-form-item label="分类名称" prop="text" required>
                <el-input placeholder="请输入分类名称" v-model="form.text"></el-input>
            </el-form-item>
            <el-form-item label="分类图片" prop="image">
                <el-input type="hidden" v-model="form.image"></el-input>
                <el-upload
                    action="admin/upload/image_upload"
                    :auto-upload="true"
                    :multiple="false"
                    :limit="1"
                    :data="{'type': 'category'}"
                    list-type="picture-card"
                    :file-list="file_list"
                    :on-success="uploadSuccess"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="fileRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item prop="parent_id" label="所属分类">
                <el-select v-model="form.parent_id" placeholder="请选择所属分类">
                    <el-option v-for="(item,index) in categories" :key="index" :label="item.text" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="is_recommand" label="是否热门分类：">
                <el-switch
                    v-model="form.is_recommand"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            <el-form-item prop="hidden" label="是否隐藏：">
                <el-switch
                    v-model="form.hidden"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            <el-form-item>
                <el-button :disabled="disabled" :loading="addLoading" type="primary" @click="submitForm('form')">立即创建</el-button>
                <el-button @click="$router.push('/category')">取消</el-button>
            </el-form-item>
        </el-form>
    </section>
</template>

<script>
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import { quillEditor } from 'vue-quill-editor'
    import { removeFile, getCategory, addCategory } from "../../api/api";
    export default {
        data () {
            return {
                addLoading: false,
                disabled: false,
                editorOption: {
                    theme: 'snow'
                },
                file_list: [],
                rules: {
                    text: [
                        { required: true, message: '请输入分类', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ]
                },
                dialogImageUrl: '',
                dialogVisible: false,
                form: {
                    text: '',
                    parent_id: '',
                    image: '',
                    is_recommand: false,
                    hidden: false
                },
                categories: [
                    {id: '', text: ''}
                ],
            }
        },
        created() {
            this.getCategory();
        },
        methods: {
            getCategory(){
                getCategory().then((res) => {
                    this.categories = res.data.data;
                })
            },
            uploadSuccess(res){
                this.form.image = res.data
            },
            fileRemove(file) {
                console.log(file);
                var file_name = file.response.data;
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    if(res.code == 200){
                        this.form.image = ''
                    }
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            submitForm(formName) {
                var this_ = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this_.addLoading = true;
                        addCategory(this_.form).then((res) => {
                            console.log(res.data);
                            if (res.code == 200){
                                this_.addLoading = false;
                                this_.disabled = true;
                                this.$message({
                                    message: res.msg,
                                    showClose: true,
                                    type: 'success',
                                    duration: 1200,
                                    onClose: function(){
                                        console.log('添加成功回调');
                                        this_.$router.push('/category')
                                    }
                                });
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        name: "category_form",
        components: {
            quillEditor
        }
    }
</script>

<style scoped>
    .main{
        width: 80%;
        /*margin-left: 5%;*/
    }
</style>
